package dao;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.security.InvalidKeyException;
import java.security.InvalidParameterException;
import java.sql.SQLException;

import org.junit.Test;

public class KernelTest {

	@Test
	public void testAuthentication() {
		try {
			Kernel k = Kernel.getKernel();
			assertEquals("Authenticating the test acc", 1,
					k.authentication("test", "test"));
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (InvalidParameterException e) {
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		}
	}

	@Test
	public void testPasswordChange() {

		try {
			Kernel k = Kernel.getKernel();
			k.passwordChange("test", "test", "test", "newTest");
			assertEquals("password should be: newTest", "newTest",
					k.getPassword("test"));
			k.passwordChange("test", "newTest", "test", "test");
			assertEquals("password should be: test", "test",
					k.getPassword("test"));
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (InvalidParameterException e) {
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		}
	}

	@Test
	public void testActivation() {
		Kernel k = null;
		
		try {
			k = Kernel.getKernel();
			k.authentication("try", "try");
		} catch (SQLException e) {
			fail("Something is wrong with the database");
			e.printStackTrace();
		} catch (InvalidParameterException e) {
			e.printStackTrace();
			try {
				assertEquals("Ativando", true,
						k.activation("test", "test", "try"));
				k.authentication("try", "try");
			} catch (InvalidParameterException e1) {
				e1.printStackTrace();
			} catch (InvalidKeyException e1) {
				e1.printStackTrace();
				fail("Something went wrong");
			} catch (SQLException e1) {
				e1.printStackTrace();
				fail("Something went wrong");
			}
		} catch (InvalidKeyException e) {
			e.printStackTrace();
			fail("Something is REALLY wrong with the database");
		}

		try {
			assertEquals("Desativando", false,
					k.deactivation("test", "test", "try"));
			k.authentication("try", "try");
		} catch (InvalidParameterException e) {
			System.out.println("That was supposed to happen anyway");
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			e.printStackTrace();
			fail("Something went wrong");
		} catch (SQLException e) {
			e.printStackTrace();
			fail("Something went wrong");
		}
	}

	@Test
	public void testInserirContrato() {
		int c;

		try {
			Kernel k = Kernel.getKernel();
			c = k.inserirContrato(0, 0);
			assertEquals("Id", c, Kernel.getKernel().obterContrato(c).id);
			assertEquals("Juros", c,
					Kernel.getKernel().obterContrato(c).taxaJuros);
			assertEquals("Honorarios", c,
					Kernel.getKernel().obterContrato(c).taxaHonorarios);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void testKernel(){
		try {
			Kernel k = Kernel.getKernel();
			assertEquals("verifying Database", true, Kernel.getKernel() != null);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
